package com.hx.xiao.controller;

import com.hx.xiao.entity.Taol;
import com.hx.xiao.service.ITaolService;
import com.hx.xiao.utils.Code;
import com.hx.xiao.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/piyaoh/taolun")
public class TaolController {

    @Autowired
    private ITaolService TaolService;

    //查询帖子
    @GetMapping("/find/{id}")
    private R findall(@PathVariable Integer id){
        List<Taol> byId = TaolService.findById(id);
        if (byId.size()>0){
            return new R(Code.GET_OK,byId,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }

    @PostMapping ("/add")
    private R addById(@RequestBody Taol taol){
        int i = TaolService.insertById(taol);
        if (i>0){
            return new R(Code.GET_OK,i,"添加成功");
        }else {
            return new R(Code.GET_ERRO,"添加失败");
        }
    }

    @GetMapping("/all")
    private R findall(){
        List<Taol> all = TaolService.findAll();
       if (all!=null){
           return new R(Code.GET_OK,all,"查询成功");
       }else {
           return new R(Code.GET_ERRO,"查询失败");
       }
    }


    @GetMapping("/fi/{id}")
    private R findBy(@PathVariable Integer id){
        Taol by = TaolService.findBy(id);
        if (by!=null){
            return new R(Code.GET_OK,by,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }

    }
    @GetMapping("/de/{id}")
    private R deleteById(@PathVariable Integer id){
        int i = TaolService.deleteById(id);
        if (i>0){
            return new R(Code.DELETE_OK,"删除成功");
        }else {
            return new R(Code.DELETE_ERRO,"删除失败");
        }
    }

    @PostMapping("/up")
    private R updateById(@RequestBody Taol taol){
        int i = TaolService.updateB(taol);
        if (i>0){
            return new R(Code.UPDATE_OK,"修改成功");
        }else {
            return new R(Code.UPDATE_ERRO,"修改失败");
        }
    }
}
